package com.governtalk.backend.controller;

import com.governtalk.backend.dto.resource.LearningResourceDto;
import com.governtalk.backend.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/api/resources")
public class ResourceController {
    
    @Autowired
    private ResourceService resourceService;
    
    @GetMapping("/categories")
    public ResponseEntity<List<LearningResourceDto>> getResourceCategories() {
        List<LearningResourceDto> categories = resourceService.getResourceCategories();
        return ResponseEntity.ok(categories);
    }
    
    @GetMapping("/search")
    public ResponseEntity<Page<LearningResourceDto>> searchResources(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String sortBy,
            @RequestParam(required = false) String sortOrder,
            Pageable pageable) {
        Page<LearningResourceDto> resources = resourceService.searchResources(keyword, type, categoryId, sortBy, sortOrder, pageable);
        return ResponseEntity.ok(resources);
    }
    
    @GetMapping("/hot")
    public ResponseEntity<List<LearningResourceDto>> getHotResources(@RequestParam(defaultValue = "10") int count) {
        List<LearningResourceDto> resources = resourceService.getHotResources(count);
        return ResponseEntity.ok(resources);
    }
    
    @GetMapping("/new")
    public ResponseEntity<List<LearningResourceDto>> getNewResources(@RequestParam(defaultValue = "10") int count) {
        List<LearningResourceDto> resources = resourceService.getNewResources(count);
        return ResponseEntity.ok(resources);
    }
    
    @GetMapping("/{resourceId}")
    public ResponseEntity<LearningResourceDto> getResourceDetail(@PathVariable Long resourceId) {
        LearningResourceDto resource = resourceService.getResourceDetail(resourceId);
        return ResponseEntity.ok(resource);
    }
    
    @PostMapping("/upload")
    public ResponseEntity<LearningResourceDto> uploadResource(
            @RequestParam("file") MultipartFile file,
            @RequestParam String title,
            @RequestParam String description,
            @RequestParam String type,
            @RequestParam(required = false) Long categoryId) {
        LearningResourceDto resource = resourceService.uploadResource(file, title, description, type, categoryId);
        return ResponseEntity.ok(resource);
    }
    
    @PostMapping("/{resourceId}/download")
    public ResponseEntity<Map<String, String>> downloadResource(@PathVariable Long resourceId) {
        String downloadUrl = resourceService.downloadResource(resourceId);
        Map<String, String> response = new HashMap<>();
        response.put("downloadUrl", downloadUrl);
        return ResponseEntity.ok(response);
    }
    
    @PostMapping("/{resourceId}/favorite")
    public ResponseEntity<Map<String, Boolean>> favoriteResource(@PathVariable Long resourceId) {
        resourceService.favoriteResource(resourceId);
        Map<String, Boolean> response = new HashMap<>();
        response.put("isFavorite", true);
        return ResponseEntity.ok(response);
    }
    
    @GetMapping("/favorites")
    public ResponseEntity<Page<LearningResourceDto>> getFavoriteResources(Pageable pageable) {
        Page<LearningResourceDto> resources = resourceService.getFavoriteResources(pageable);
        return ResponseEntity.ok(resources);
    }
    
    @GetMapping("/{resourceId}/reviews")
    public ResponseEntity<Page<LearningResourceDto>> getResourceReviews(@PathVariable Long resourceId, Pageable pageable) {
        Page<LearningResourceDto> reviews = resourceService.getResourceReviews(resourceId, pageable);
        return ResponseEntity.ok(reviews);
    }
    
    @PostMapping("/{resourceId}/reviews")
    public ResponseEntity<String> addResourceReview(
            @PathVariable Long resourceId,
            @RequestBody Map<String, Object> reviewData) {
        Integer rating = (Integer) reviewData.get("rating");
        String content = (String) reviewData.get("content");
        resourceService.addResourceReview(resourceId, rating, content);
        return ResponseEntity.ok("Review added successfully");
    }
}
